package com.longrise.android.byjk.plugins.tabthird.personal;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.ViewGroup;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.R;
import com.longrise.android.byjk.plugins.vip.integralexchange.IntegralExchangeImageActivity;
import com.longrise.android.byjk.widget.view.BBswipeRefreshLayout;
import com.longrise.common.base.BaseFragment;

import org.greenrobot.eventbus.EventBus;

/**
 * Created by Administrator on 2018/3/6.
 *更换背景--每个type下的详情页
 *
 */

public class ChangeSeniorWallDeatilFragment extends BaseFragment<ChangeSeniorWallDeatilPresenter>
        implements SwipeRefreshLayout.OnRefreshListener, ChangeSeniorWallDetailContract.View, ChangeSeniorDeatilAdapter.changeSeniorDetailListener {

    public static final String BACKGROUNDID = "backgroundid";
    private BBswipeRefreshLayout mSrl;
    private RecyclerView mRcv;
    private String mWallId;
    private ChangeSeniorDeatilAdapter mAdapter;
    private int mPosition;
    private int INTEGRALEXCHANGEIMAGERESQCODE = 100;

    @Override
    protected int getLayoutResourse(@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_change_senior_detail;
    }

    @Override
    public void initView() {

        mSrl = (BBswipeRefreshLayout) mRootView.findViewById(R.id.change_senior_detail_sp);
        mRcv = (RecyclerView) mRootView.findViewById(R.id.change_senior_detail_rv);
        Bundle arguments = getArguments();
        mWallId = arguments.getString(BACKGROUNDID);
        initRcv();
        initEvent();
        mPresenter.getBackgroundWallData(mWallId);
    }

    private void initRcv() {

//        View headView = View.inflate(mContext, R.layout.item_rcv_integralexchange_head, null);
        mAdapter = new ChangeSeniorDeatilAdapter();
//        mAdapter.addHeaderView(headView);
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        mRcv.setLayoutManager(layoutManager);
        mRcv.setAdapter(mAdapter);
    }

    private void initEvent() {
        mSrl.setOnRefreshListener(this);
        mAdapter.setOnRcvItemClickListener(this);
    }


    @Override
    public void onRefresh() {
        mPresenter.getBackgroundWallData(mWallId);
    }

    @Override
    public void onClick(int type, String lockstate, String goodsid, String imageUrl, String detailpicurl, int position) {
        this.mPosition = position;
        switch (type) {
            case 1:
                toTextClick(lockstate, goodsid);
                break;
            case 2:
                toImageClick(imageUrl, lockstate, detailpicurl, goodsid);
                break;
        }
    }

    @Override
    public void refreshWallData(EntityBean bean) {
        EntityBean[] goodsinfos = bean.getBeans("result");
        mAdapter.setDatas(goodsinfos);
    }

    @Override
    public void setRefreshing(boolean isRefresh) {
        mSrl.setRefreshing(isRefresh);
    }

    @Override
    public void responseChangeSkin() {

        ChangeWallEvent event = new ChangeWallEvent();
        event.setRefresh(true);
        EventBus.getDefault().post(event);
    }

    /**
     * 点击的是开始使用
     */
    private void toTextClick(String lockstate, String goodsid) {
        if ("1".equals(lockstate)) { //解锁状态  1:已解锁  0：未解锁
            mPresenter.toChangeSkin(goodsid);
        }
    }

    /**
     * 点击的是预览
     */
    private void toImageClick(String imageUrl, String lockstate, String detailpicurl, String goodsid) {
        Bundle bundle = new Bundle();
        bundle.putString(IntegralExchangeImageActivity.IMAGEURL_INTENT, imageUrl);
        bundle.putString(IntegralExchangeImageActivity.LOCKSTATE_INTENT, lockstate);
//        bundle.putString(IntegralExchangeImageActivity.NEEDSCORS_INTENT, needscores);
        bundle.putString(IntegralExchangeImageActivity.DETAILPICURL_INTENT, detailpicurl);
        bundle.putString(IntegralExchangeImageActivity.GOODSID_INTENT, goodsid);
//        bundle.putString(IntegralExchangeImageActivity.GOODSTYPE_INTENT, goodstype);
        bundle.putInt(IntegralExchangeImageActivity.CURRENT_BACK_POSTION, mPosition);  //当前背景position
//        bundle.putInt(IntegralExchangeActivity.PAGE_FROM_FLAG, PAGE_FROM_PERSONALEX);
        Intent intent = new Intent(mContext, IntegralExchangeImageActivity.class);
        intent.putExtras(bundle);
        startActivityForResult(intent, INTEGRALEXCHANGEIMAGERESQCODE);
    }


    @Override
    public void showLoadingDialog() {

    }

    @Override
    public void dismissLoadingDialog() {

    }

}
